//登录与注册
import {
    reqAddHot,
    reqCategoryList,
    reqChatAI,
    reqChatGpt3,
    reqChatGpt35,
    reqChatRasa,
    reqEntityQuestions, reqGetIp,
    reqGetMessages,
    reqGetUsers,
    reqGetVideoDetail,
    reqGetVideoList,
    reqGoodResponse,
    reqHotQuestions,
    reqQuestionByMid,
    reqUpdateMessage,
    reqUpdateUser,
    reqUserInfo,
    reqUserLogin,
    reqUserRegister,
    reqUpdateIp, reqGetHot, reqUpdateHot, reqGetMessagesByGood, reqGetMessageByUid, reqGetDiseaseList
} from "@/api";
import {getToken, removeToken, setToken} from "@/utils/token";
import {removeQuestionList, removeUserInfo, setUserInfo} from "@/utils/userInfo";

const state = {
    code: "",
    token: getToken(),
    userInfo: {
        userInfo: {
            email: "",
            username: "",
            uid: "",
            password: ""
        }
    }
}
const mutations = {
    GETCODE(state, code) {
        state.code = code
    },
    USERLOGIN(state, token) {
        state.token = token
    },
    GETUSERINFO(state, userInfo) {
        state.userInfo = userInfo
    },
    //清除本地数据
    CLEAR(state) {
        state.token = '';
        state.userInfo = {}
        //本地存储清空
        removeToken()
        removeUserInfo()
        removeQuestionList()
    }
}
const actions = {

    //用户注册
    // eslint-disable-next-line no-unused-vars
    async userRegister({commit}, user) {
        let result = await reqUserRegister(user);
        if (result.code === 20000) {
            return 'ok'
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //登录业务
    async userLogin({commit}, data) {
        let result = await reqUserLogin(data)
        //服务器下发token，将来经常通过token找服务器要用户信息进行展示
        if (result.code === 20000) {
            commit("USERLOGIN", result.data.token)
            //持久化存储token
            setToken(result.data.token)
            //用户已经登陆成功且获取到token
            let userInfo = reqUserInfo()
            setUserInfo(userInfo)
            return 'ok'

        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //Rasa对话
    // eslint-disable-next-line no-unused-vars
    async rasaChat({commit}, data) {
        let result = await reqChatRasa(data)
        if (result.data == null) {
            alert("rasa服务器响应失败")
        }
        return result.data
    },
    //获取具体video信息
    // eslint-disable-next-line no-unused-vars
    async getVideoDetail({commit}, data) {
        let result = await reqGetVideoDetail(data.vod_id)
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //通过疾病类型获取videoList
    // eslint-disable-next-line no-unused-vars
    async getVideoList({commit}, data) {
        let result = await reqGetVideoList(data.disease)
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    // eslint-disable-next-line no-unused-vars
    async getUserInfo() {
        let result = await reqUserInfo()
        if (result.code === 20000) {
            //存储本地userInfo 为jsOn
            let userInfo = JSON.stringify(result.data.userInfo);
            setUserInfo(userInfo)
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //退出登录
    async userLogout({commit}) {
        commit("CLEAR")
    },
    //获取二级菜单
    async categoryList() {
        let result = await reqCategoryList();
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //增加热度
    async addHot({commit}, data) {
        let result = await reqAddHot(data.attachment);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //获取热门问答
    async getHotQuestions({commit}, data) {
        let result = await reqHotQuestions(data.countEntity, data.countQuestion);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //获取实体、问答
    async getEntityQuestions({commit}, data) {
        let result = await reqEntityQuestions(data.entity, data.countQuestions);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //获取问答
    async getQuestionInfo({commit}, data) {
        let result = await reqQuestionByMid(data.mid);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //获取最佳回复
    async getGoodResponse({commit}, data) {
        let result = await reqGoodResponse(data.count);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //ChatGpt35
    async ChatGpt35({commit}, data) {
        let result = await reqChatGpt35(data);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //ChatGpt3
    async ChatGpt3({commit}, data) {
        let result = await reqChatGpt3(data);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //ChatAI
    async ChatAI({commit}, data) {
        let result = await reqChatAI(data);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //分页获取问答
    async getMessageList({commit}, data) {
        let result = await reqGetMessages(data.current, data.limit);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //分页获取问答
    async getMessageListByGood({commit}, data) {
        let result = await reqGetMessagesByGood(data.current, data.limit);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //分页获取User
    async getUserList({commit}, data) {
        let result = await reqGetUsers(data.current, data.limit);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //修改用户信息
    async updateUserInfo({commit}, data) {
        let result = await reqUpdateUser(data);
        if (result.code === 20000) {
            return result.message
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //修改用户信息
    async updateMessageInfo({commit}, data) {
        let result = await reqUpdateMessage(data);
        if (result.code === 20000) {
            return result.message
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //分页获取Ip
    async getIpList({commit}, data) {
        let result = await reqGetIp(data.current, data.limit);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },

    //修改Ip信息
    async updateIpInfo({commit}, data) {
        let result = await reqUpdateIp(data);
        if (result.code === 20000) {
            return result.message
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //分页获取Hot
    async getHotList({commit}, data) {
        let result = await reqGetHot(data.current, data.limit);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },

    //修改hot信息
    async updateHotInfo({commit}, data) {
        let result = await reqUpdateHot(data)
        if (result.code === 20000) {
            return result.message
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //根据uid获取Message
    async getMessageByUid({commit}, data) {
        let result = await reqGetMessageByUid(data.current,data.limit,data.uid);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
    //根据uid获取Message
    async getDiseaseList({commit}, data) {
        let result = await reqGetDiseaseList(data.name);
        if (result.code === 20000) {
            return result.data
        } else {
            alert(result.message)
            return Promise.reject(new Error(result.message))
        }
    },
}
const getters = {}
export default {
    state,
    mutations,
    actions,
    getters
}
